<template>
  <div class="Class">
    <el-card class="card">
      <el-input
        :prefix-icon="MessageBox"
        size="large"
        v-model="inputClass"
        @keydown.enter="handelAddClass"
        placeholder="请输入要新增的选课"
        style="margin: 10px; width: 250px"
      >
        <template #append>
          <el-tooltip content="添加选课" effect="dark" placement="right">
            <div
              @click="handelAddClass"
              style="width: 100%; height: 100%; padding: 0 12px"
            >
              <el-icon><CirclePlus /></el-icon>
            </div>
          </el-tooltip>
        </template>
      </el-input>
      <el-table
        :data="tableData"
        size="large"
        style="width: 100%; margin-bottom: 20px"
      >
        <el-table-column prop="id" label="课程ID" align="center" width="100" />
        <el-table-column prop="schoolName" label="课程名称" align="center" />
        <el-table-column label="操作" align="center" width="100px">
          <template #default="{ row }">
            <el-tooltip effect="dark" content="删除历史" placement="top">
              <el-button
                type="danger"
                @click="handleDeleteSchool(row)"
                circle
                icon="delete"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-show="total"
        v-model:current-page="pageNO"
        v-model:page-size="pageSize"
        layout="prev, pager, next, ->,total, sizes, jumper "
        :page-sizes="[5, 10, 15, 20]"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="getAllSchool"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { reqGetAllSchool, reqAddSchool, reqDeleSchool } from '@/api/class'
import { MessageBox } from '@element-plus/icons-vue'

// 表单数据
const tableData = ref<any>([])
const pageSize = ref(5)
const pageNO = ref(1)
const total = ref(0)
const inputClass = ref('')

// 获取全部课程
const getAllSchool = async (page = 1) => {
  pageNO.value = page
  const res = await reqGetAllSchool({
    pageSize: pageSize.value,
    pageNO: pageNO.value,
  })
  if (res.code === 200) {
    tableData.value = res.data.records
    total.value = res.data.total
  }
}

// 翻页
const handleSizeChange = () => {
  getAllSchool()
}

// 添加课程
const handelAddClass = async () => {
  if (inputClass.value) {
    const res = await reqAddSchool({ schoolName: inputClass.value })
    inputClass.value = ''
    if (res.code === 200) {
      getAllSchool(pageSize.value)
      ElMessage.success('添加成功！')
    } else {
      ElMessage.error('添加失败！')
    }
  } else {
    ElMessage.error('课程名称不能为空')
  }
}

// 删除课程
const handleDeleteSchool = async (row: any) => {
  const res = await reqDeleSchool({ id: row.id })
  console.log(res)
  if (res.code === 200) {
    ElMessage.success('删除成功！')
    getAllSchool(tableData.value.length > 1 ? pageNO.value : pageNO.value - 1)
  } else {
    ElMessage.error('删除失败！')
  }
}

onMounted(() => {
  getAllSchool()
})
</script>

<style lang="scss" scoped>
.Class {
  padding: 20px;

  & > .card {
    min-height: calc(100vh - 88px);
  }
}

:deep(.el-input-group__append, .el-input-group__prepend) {
  padding: 0;
}
</style>
